﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using WmsApp.Service;
using WmsApp.Style;
using WmsApp.UI;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormInvoice : BaseForm
    {
        public FormInvoice()
        {
            InitializeComponent();
            this.GridControlInvoice.DataSource = new List<ExtendInvoiceBill>();
        }

        public override  string HeadText
        {
            get
            {
                return "发票管理";
            }
        }
        
        private void CmdQuery2_Click(object sender, EventArgs e)
        {
            try
            {
                IEntityService<ExtendInvoiceBill, ExtendInvoiceBillService> service =
                EntityServiceFactory.GetEntityService<ExtendInvoiceBill, ExtendInvoiceBillService>();
                IQueryControl queryControl = this.qInvoiceBill1;
                this.GridControlInvoice.DataSource = service.GetEntityList(queryControl.GetCondition());
                RowStyleConditionController.AddStyleFormatCondition(this.gridView2, "InvoiceBillStateDesc", "[InvoiceBillStateDesc] == '部分已付款'");
                RowStyleConditionController.AddStyleFormatCondition(this.gridView2, "InvoiceBillStateDesc", "[InvoiceBillStateDesc] == '全部已付款'",Color.LightBlue);
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdNew2_Click(object sender, EventArgs e)
        {
            var form = new FormInvoiceEditor();
            form.ShowDialog();
            if(form.DialogResult == DialogResult.OK)
            {
                AddExtendInvoiceBillToGrid(form.FInvoiceBill.InvoiceNo);
            }
        }

        private void AddExtendInvoiceBillToGrid(string invoiceNo)
        {
            IEntityService<ExtendInvoiceBill, ExtendInvoiceBillService> service =
         EntityServiceFactory.GetEntityService<ExtendInvoiceBill, ExtendInvoiceBillService>();
            ExtendInvoiceBill extendInvoiceBill = service.GetEntity(invoiceNo);

            //从列表中找
            ExtendInvoiceBill findObj =
                ((List<ExtendInvoiceBill>)this.GridControlInvoice.DataSource).Find((e) => e.InvoiceNo == extendInvoiceBill.InvoiceNo);
            if(findObj == null)
            {
                 ((List<ExtendInvoiceBill>)this.GridControlInvoice.DataSource).Add(extendInvoiceBill);
                this.GridControlInvoice.RefreshDataSource();
            }
            else
            {
                ((List<ExtendInvoiceBill>) this.GridControlInvoice.DataSource).Remove(findObj);
                ((List<ExtendInvoiceBill>)this.GridControlInvoice.DataSource).Add(extendInvoiceBill);
                this.GridControlInvoice.RefreshDataSource();
            }
        }

        private void CmdDeleteInvoice_Click(object sender, EventArgs e)
        {
            try
            {
                var invoiceBill = (ExtendInvoiceBill)this.gridView2.GetRow(this.gridView2.FocusedRowHandle);
                if (invoiceBill != null)
                {
                    if (DialogResult.OK == XtraMessageBox.Show("确实要彻底删除除该发票吗？发票号：" + invoiceBill.InvoiceNo, "提问", MessageBoxButtons.OKCancel, MessageBoxIcon.Question))
                    {
                        Factory.WmsServiceFactory.GetServiceClient().DeleteInvoiceBill(invoiceBill.InvoiceNo);
                        ((List<ExtendInvoiceBill>)this.gridView2.DataSource).Remove(invoiceBill);
                        this.GridControlInvoice.RefreshDataSource();
                        XtraMessageBox.Show("删除发票成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
    
        }

        private void CmdEdit_Click(object sender, EventArgs e)
        {
            try
            {
                var invoiceBill = (ExtendInvoiceBill)this.gridView2.GetRow(this.gridView2.FocusedRowHandle);
                if (invoiceBill != null)
                {
                    var invoiceBillInstance =
                        Factory.WmsServiceFactory.GetServiceClient().GetInvoiceBill(invoiceBill.InvoiceNo);
                    invoiceBillInstance.BindPropertyChanged();
                    var form = new FormInvoiceEditor(invoiceBillInstance);
                    form.ShowDialog();
                    if (form.DialogResult == DialogResult.OK)
                    {
                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).AmountTax =
                            form.FInvoiceBill.AmountTax;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).ProviderCode =
                            form.FInvoiceBill.ProviderCode;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).ProviderName =
                            form.FInvoiceBill.ProviderName;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).ExtDateTime2 =
                            form.FInvoiceBill.ExtDateTime2;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).ExtVarChar1 =
                            form.FInvoiceBill.ExtVarChar1;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).ExtVarChar2 =
                            form.FInvoiceBill.ExtVarChar2;

                        ((ExtendInvoiceBill) this.gridView2.GetRow(this.gridView2.FocusedRowHandle)).WcfEntityState =
                            EntityState.Unchanged;

                        this.gridView2.RefreshData();
                    }
                }
            }
            catch (Exception ex)
            {
                Exceptions.ExceptionHandler.ShowException(ex);
            }
        }
    }
}